<?php

namespace App\Admin\Tools;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Illuminate\Http\Request;

class SignExporterTools extends AbstractTool
{
    /**
     * @return string
     */
    protected $title = '导出当前页';

    protected $request_param = [];

    protected $request_filename = '';

    /**
     * 接收参数
     */
    public function __construct($param = null, $filename = null, $title)
    {
        $this->request_param = $param;
        $this->request_filename = $filename;
        parent::__construct($title);

        $this->title = $title;
    }

    /**
     * 按钮样式定义，默认 btn btn-white waves-effect
     *
     * @var string
     */
    protected $style = 'btn btn-outline-info';

    /**
     * 按钮文本
     *
     * @return string|void
     */
    public function title()
    {
        return $this->title;
    }

    /**
     * 处理请求
     * 如果你的类中包含了此方法，则点击按钮后会自动向后端发起ajax请求，并且会通过此方法处理请求逻辑
     *
     * @param Request $request
     */
    public function handle(Request $request)
    {
        $param = $request->get('param');
        $filename = $request->get('filename');

        // 调用/export接口进行导出
        return $this->response()->download('/export?filename=' . $filename . '&param=' . json_encode($param) . '&_export_=1');
    }

    /**
     * 设置请求参数
     *
     * @return array|void
     */
    public function parameters()
    {
        return [
            'param' => $this->request_param,
            'filename' => $this->request_filename,
            'title' => $this->title,
        ];
    }
}
